package so.sao.code.service.infrastructure;

import org.springframework.transaction.support.TransactionSynchronizationAdapter;
import org.springframework.transaction.support.TransactionSynchronizationManager;

/**
 * Created by Administrator on 2017/10/31.
 */
public class EventsPublisher {

    /**
     * 内部消息 控制在方法上
     * */
    public static void publish(Object msg) {
        InternalEventStore.save(msg);
        WebContext.publisher.publishEvent(msg);
    }


    //对外
    public static void o_publish(Object msg) {
        if (TransactionSynchronizationManager.isActualTransactionActive()) {
            TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() {
                @Override
                public void afterCommit() {
                    super.afterCommit();
                    InternalEventStore.put(msg);
                }
            });
        } else {
            InternalEventStore.put(msg);
        }
    }
}
